package com.carrot.api.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.SysPointHistoryMapDto;
import com.carrot.common.core.domain.entity.SysPointHistoryMap;
import com.carrot.common.core.domain.vo.SysPointHistoryMapVo;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.Page;
import com.carrot.common.utils.StringUtils;
import com.carrot.system.service.ISysPointHistoryMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 点表历史映射字段
 *
 * @author liuhongzhen
 */
@RestController
@RequestMapping("/system/point-history")
public class BasicsHistoryMapController extends BaseController {

    @Autowired
    private ISysPointHistoryMapService historyMapService;

    @PostMapping("/list")
    public AjaxResult list(@Validated @RequestBody SysPointHistoryMapDto dto) {

        Page<SysPointHistoryMap, SysPointHistoryMapVo> page =
                new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<SysPointHistoryMap> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(dto.getEquipmentType())) {
            queryWrapper.eq("equipment_type", dto.getEquipmentType());
        }

        if (StringUtils.isNotEmpty(dto.getSearchContent())) {
            queryWrapper
                    .like("description", dto.getSearchContent())
                    .or()
                    .like("map_field", dto.getSearchContent());
        }
        queryWrapper.orderByAsc("create_time");
        page.setQueryWrapper(queryWrapper);

        page = historyMapService.findPage(page);

        return AjaxResult.success(page.getPageData());
    }

    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("id") Long id) {

        SysPointHistoryMap historyMap = historyMapService.getById(id);
        return AjaxResult.success(historyMap);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysPointHistoryMap pointHistoryMap) {

        historyMapService.add(pointHistoryMap);
        return AjaxResult.success();
    }


    @PostMapping("/update")
    public AjaxResult update(@RequestBody SysPointHistoryMap pointHistoryMap) {

        historyMapService.update(pointHistoryMap);
        return AjaxResult.success();
    }


    @GetMapping("/del")
    public AjaxResult del(@RequestParam("id") Long id) {
        historyMapService.del(id);
        return AjaxResult.success();
    }


    @PostMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody SysPointHistoryMapDto dto) {
        if (CollUtil.isEmpty(dto.getIds())) {
            throw new ServiceException("参数校验失败");
        }
        historyMapService.batchDel(dto.getIds());
        return AjaxResult.success();
    }
}
